<?php

class m_mapping extends CI_Model{
	
	function _list($data = array()){
		extract($data);
		$sql = "SELECT monitor_id,
            proyek_nama, mandor_nama, pekerjaan_ket,
            t_monitor.m_proyek_id, m_mandor_id, monitor_selesai, m_pekerjaan_id,
            t_mapping.m_proyek_id to_proyek_id, t_mapping.mapping_tglmulai,
            t_mapping.mapping_tglselesai FROM t_monitor
            LEFT JOIN m_proyek ON t_monitor.m_proyek_id = proyek_id
            LEFT JOIN m_mandor ON m_mandor_id = mandor_id
            LEFT JOIN m_pekerjaan ON m_pekerjaan_id = pekerjaan_id
            LEFT JOIN t_mapping ON monitor_id = t_monitor_id
            WHERE monitor_selesai >= now() 
            ORDER BY monitor_selesai ASC
		";

		
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return$result;
	}
	
	function _insert($data){
		$this->db->insert('t_monitor',$data);
		return $this->db->insert_id();
	}
	
	function _update($data,$id){
		$this->db->where('t_monitor_id',$id);
        $query = $this->db->get("t_mapping");
        if ($query->num_rows() > 0){
		  $this->db->where('t_monitor_id',$id);
		  $this->db->update('t_mapping',$data);
        }else{
          $this->db->set("t_monitor_id",$id);
		  $this->db->insert('t_mapping',$data);
        }
		return 1;
	}
	
	function _update2($data,$id){
		$this->db->where('t_monitor_id',$id);
        $query = $this->db->get("t_mapping");
        if ($query->num_rows() > 0){
		  $this->db->where('t_monitor_id',$id);
		  $this->db->update('t_mapping',$data);
        }else{
          $this->db->set("t_monitor_id",$id);
		  $this->db->insert('t_mapping',$data);
        }
		return 1;
	}
    
	function _delete($id){
		$this->db->where('monitor_id',$id);
		$this->db->delete('t_monitor');
		return 1;
	}
	
	function _deactivate($data,$id){
		$this->db->where('monitor_id',$id);
		$this->db->update('t_monitor',$data);
		return 1;
	}
}